var num = 1;

function editable(num){
	var nodoPadre = document.getElementById("filaAl" + num);
	var contador = 1;
	var nodo;
	var disabled;
	if(nodoPadre.childNodes[0].childNodes[0].checked){
		disabled = false;
	}
	else{
		disabled = true;
	}
	while((nodo = nodoPadre.childNodes[contador]) != null){
		nodo = nodo.childNodes[0];
		nodo.disabled = disabled;
		contador++;
	}
	nodoPadre.childNodes[contador - 1].childNodes[0].disabled = !disabled;
}

function eliminar(num){
	var nodoPadre = document.getElementById("filaAl" + num);
	var delet = document.getElementById("delete" + num);
	
	editable(num);
	var checke;
	var styl;
	if(!delet.checked){
		checke = false;
		styl = "";
	}
	else{
		checke = true;
		styl="red";
	}
	nodoPadre.childNodes[0].childNodes[0].disabled = checke;
	var contador = 1;
	nodoPadre.style.backgroundColor=styl;
	while((nodo = nodoPadre.childNodes[contador]) != null){
		
		contador++;
	}
	
	nodoPadre.childNodes[contador - 1].childNodes[0].disabled = false;
}

/*AUMENTAR/QUITAR CAMPO*/

function atr(id){
	var auto = document.getElementById('a_i'+id);
	var atributo = document.getElementById('atri' + id); 
	if(atributo.value != ""){
		auto.disabled = false;
	}
	else{
		auto.disabled = true;
		auto.checked = false;
	}
}




function aumentarCampo(){
	var tabla = document.getElementById('crearTabla').childNodes[0];
	var tr = document.createElement('tr');
	num++;
	tr.setAttribute('id', 'fila'+ num);
	tabla.appendChild(tr);
	tr = document.getElementById('fila'+num);
	var contenido = tabla.childNodes[1].innerHTML;
   	tr.innerHTML = contenido;
   	var contador = 0;
   	var a;
   	var b = '';
   	while((a = tr.childNodes[contador]) != null ){
   		a = a.childNodes[0];
   		a.setAttribute('name', a.name.substring(0, (a.name.length - 1)) + num);
   		if(a.name == 'atr'+num){
   			a.setAttribute('onchange','atr('+num+')');
   			a.setAttribute('id','atri'+num);
   		}
   		else if(a.name == 'a_i' + num){
   			a.setAttribute('id','a_i'+num);
   		}
   		b = b + a.innerHTML;
   		contador++;
   	}
   	if(contador > 0){
   		restartButton(num);
   	}
}

function quitarCampo(){
	if(num>1){
		var tabla = document.getElementById('crearTabla');
		//tabla.childNodes[0].childNodes[num].innerHTML = '';
		var nodo = tabla.childNodes[0]; 
		tabla.childNodes[0].removeChild(nodo.childNodes[num]);
		restartButton(num - 1);
		num--;
	}
}

function restartButton(numero){
	document.getElementById('aniadir').setAttribute('onclick','aumentarCampo('+numero+')');
	document.getElementById('quitar').setAttribute('onclick','quitarCampo('+numero+')');
	if(numero == 1){
		document.getElementById('quitar').disabled = true;
	}else{
		document.getElementById('quitar').disabled = false;
	}
}

function insertarOc(){
	var doc = document.getElementById('nuevoCampo');
	var check = document.getElementById('insertarCampo');
	if(check.checked){
		doc.setAttribute("class", "visible");
	}
	else{
		doc.setAttribute("class", "oculto");
	}
}


function rTabla(){
	var div = document.getElementById('rLaTabla');
	if(document.getElementById('renombrarTabla').checked){
		div.innerHTML = "<br /><table><tr>" +
				"<td>Nombre de la tabla:</td>" +
				"<td><input type='text' name='nuevoNombreTabla' class='campo' /></td></tr></table>";
	}
	else{
		div.innerHTML = "";
	}
}